package com.dog.ui;

import com.dog.threesixty.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class LearningView extends View {

	private Paint backgroundPaint;
	private Paint selectedPaint;
	private Paint dividerPaint;
	// private Paint linePaint;
	private Paint textPaint;
	private Paint titlePaint;
	private TextPaint scoreTextPaint;

	private int cellHeight;
	private int cellWidth;

	private float textFontSize = 12.0f;
	private float scoreFontSize = 10.0f;

	private int leftMargin = 15;

	private float posX = -1;
	private float posY = -1;
	private float movedX = 0;
	private float movedY = 0;

	private int h;

	private Bitmap lockedBitmap;
	private Bitmap backgroundBitmap;
	private Bitmap dividerBitmap;
	private Bitmap selectedBitmap;
	private Paint lockedPaint;
	private Paint scorePaint;

	private static int selectedIndex;

	public static int itemPosition;

	private boolean isLevelUnlocked[] = new boolean[18];
	private int score[] = new int[18];
	private int percent[] = new int[18];

	private String cellName[] = { "General 1", "General 2", "General 3",
			"Conformation 1", "Conformation 2", "Conformation 3",
			"Conformation 4", "Skeleton 1", "Skeleton 2",
			"Superficial Muscles 1", "Superficial Muscles 2", "Deep Muscles 1",
			"Deep Muscles 2", "Skull", "Teeth", "Organs 1", "Organs 2",
			"Random Challenge" };
	private String title[] = new String[21];
	private TextPaint titleTextPaint;

	public LearningView(Context context) {
		super(context);
		initialize();
	}

	public LearningView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initialize();
	}

	public LearningView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initialize();
	}

	public void setLockedLevel(boolean isLevelUnLocked[]) {
		this.isLevelUnlocked = isLevelUnLocked;
	}

	public void setScore(int score[], int percent[]) {
		this.score = score;
		this.percent = percent;
	}

	private void initialize() {
		backgroundPaint = new Paint();
		backgroundPaint.setColor(Color.parseColor("#666259"));

		selectedPaint = new Paint();
		selectedPaint.setColor(Color.parseColor("#827F79"));

		dividerPaint = new Paint();
		dividerPaint.setColor(Color.parseColor("#827F79"));
		dividerPaint.setAntiAlias(true);

		// linePaint.setColor(Color.parseColor("#7F7B74"));

		textPaint = new Paint();
		textPaint.setColor(Color.WHITE);
		textPaint.setTextSize(textFontSize);
		textPaint.setTextAlign(Align.LEFT);
		textPaint.setStyle(Style.FILL);
		textPaint.setAntiAlias(true);

		scoreTextPaint = new TextPaint();
		scoreTextPaint.setTextSize(10.0f);

		titlePaint = new Paint();
		titlePaint.setColor(Color.WHITE);
		titlePaint.setTextSize(textFontSize);
		titlePaint.setTextAlign(Align.LEFT);
		// textPaint.setStyle(Style.FILL);
		titlePaint.setAntiAlias(true);

		titleTextPaint = new TextPaint();
		titleTextPaint.setTextSize(14.0f);

		scorePaint = new Paint();
		scorePaint.setColor(Color.WHITE);
		scorePaint.setTextSize(scoreFontSize);
		scorePaint.setTextAlign(Align.LEFT);
		scorePaint.setStyle(Style.FILL);
		scorePaint.setAntiAlias(true);

		lockedBitmap = BitmapFactory.decodeResource(getResources(),
				android.R.drawable.ic_lock_lock);

		backgroundBitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.cell_bkg);

		selectedBitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.cell_overlay);
		dividerBitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.divider);

		lockedPaint = new Paint();
		lockedPaint.setColor(Color.BLUE);
		lockedPaint.setAntiAlias(true);
		selectedIndex = 0;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		cellHeight = getHeight() / 5;
		cellWidth = getWidth();

		canvas.save();
		// canvas.drawRect(0, 0, getWidth(), getHeight(), backgroundPaint);
//		canvas.drawBitmap(backgroundBitmap, getWidth(), getHeight(),
//				lockedPaint);
		canvas.translate(0, movedY);
		canvas.translate(0, -movedY);
		canvas.restore();

		canvas.translate(0, movedY);

		h = cellHeight * (cellName.length + 4);

		for (int i = 0; i < cellName.length; i++) {
			title[i] = TextUtils.ellipsize(cellName[i], titleTextPaint,
					(float) (getWidth() - (lockedBitmap.getWidth() + 10)),
					TextUtils.TruncateAt.END).toString();
		}
		for (int i = 0; i < cellName.length; i++) {
//			canvas.drawBitmap(dividerBitmap, 0, cellHeight * (i + 2),
//					dividerPaint);
			if (i == selectedIndex) {
					canvas.drawBitmap(selectedBitmap, 0, cellHeight * (i + 2),
						selectedPaint);

			}

			if (score[i] > 0) {
				canvas.drawText(title[i], leftMargin, cellHeight * (i + 2)
						+ (cellHeight + titlePaint.getTextSize()) / 4,
						titlePaint);
				canvas.drawText("Score:" + score[i] + " @ " + percent[i]
						+ "% Accuracy", leftMargin, cellHeight * (i + 2)
						+ (cellHeight + textPaint.getTextSize()) / 4
						+ textPaint.getTextSize() + 5, scorePaint);

			} else {
				canvas.drawText(title[i], leftMargin, cellHeight * (i + 2)
						+ (cellHeight + textPaint.getTextSize()) / 2, textPaint);
			}

			if ((i >= 4) && (i != 17)) {
				if (!isLevelUnlocked[i]) {
					int leftPosition = lockedBitmap.getWidth() + 5;
					int bitmapHeight = lockedBitmap.getHeight();
					canvas.drawBitmap(lockedBitmap, cellWidth - leftPosition,
							(cellHeight * (i + 2))
									+ ((cellHeight - bitmapHeight) / 2),
							lockedPaint);
				}

			}
		}

	}

	private float downX;
	private float downY;

	public static int getSelectedIndex() {
		return selectedIndex;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int actionPerformed = event.getAction();

		cellHeight = getHeight() / 5;
		switch (actionPerformed) {
		case MotionEvent.ACTION_DOWN:
			posX = event.getX();
			posY = event.getY();
			downX = posX;
			downY = posY;
			invalidate();
		case MotionEvent.ACTION_MOVE:
			if (posX != -1 && posY != -1) {

				int diffY = h - getHeight();
				if (diffY > 0) {
					movedY = Math.min(0,
							Math.max(-diffY, movedY + event.getY() - posY));

				}

				posX = event.getX();
				posY = event.getY();
				invalidate();
			}
			break;
		case MotionEvent.ACTION_UP:
			posX = event.getX();
			posY = event.getY();

			float d = ((downX - posX) * (downX - posX))
					+ ((downY - posY) * (downY - posY));

			if (d > (cellHeight * cellHeight) / 4) {
				if (cellName != null) {
					selectedIndex = Math
							.min(cellName.length - 1,
									Math.max(
											0,
											(int) ((((getHeight() / 2) - movedY) / cellHeight) - 2)));
					movedY = -(selectedIndex * cellHeight);
					invalidate();
				}
			} else {
				if (cellName != null) {
					selectedIndex = Math.min(cellName.length - 1, Math.max(0,
							(int) (((posY - movedY) / cellHeight) - 2)));
					movedY = -(selectedIndex * cellHeight);
					invalidate();
				}
			}

			break;
		default:
			break;
		}
		return true;
	}

}
